package com.kinon.markett2.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.kinon.markett2.MyApplication;
import com.kinon.markett2.R;
import com.kinon.markett2.bluetoothprint.BluetoothPrintFormatUtil;
import com.kinon.markett2.bluetoothprint.BluetoothService;
import com.kinon.markett2.bluetoothprint.DeviceListActivity;
import com.kinon.markett2.bluetoothprint.PicFromPrintUtils;
import com.kinon.markett2.entity.GoodsInfoBean;
import com.kinon.markett2.entity.OrderEntity;
import com.kinon.markett2.tools.HttpTools;
import com.kinon.markett2.tools.NetWorkUtil;
import com.kinon.markett2.tools.ToastUtil;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;

//import com.kinon.markett2.tools.ToastUtil;

/**
 * @author：郑谦
 * @时间：2017-3-16 下午5:38:13
 * @类名：DingdanFragment.java
 * @功能: 晚餐订单（打印）
 */
public class OrderFragment extends Fragment {

    public static final int MESSAGE_STATE_CHANGE = 1;
    public static final int MESSAGE_READ = 2;
    public static final int MESSAGE_WRITE = 3;
    public static final int MESSAGE_DEVICE_NAME = 4;
    public static final int MESSAGE_TOAST = 5;
    // Key names received from the BluetoothService Handler
    public static final String DEVICE_NAME = "device_name";
    public static final String TOAST = "toast";
    // Intent request codes
    private static final int REQUEST_CONNECT_DEVICE = 1;
    private static final int REQUEST_ENABLE_BT = 2;
    // Member object for the services
    public static BluetoothService mService = null;
    View iscatview = null;
    HttpTools toolModel = null;
    String token;
    HttpTools httpTools = null;
    DayingAdapter dayingAdapter;
    BluetoothPrintFormatUtil printFormatUtil = null;
    LinkedHashMap<String, LinkedList<String>> menuMsgMap;
    LinkedList<String> gonds;
    ArrayList<GoodsInfoBean> detaillist;
    ToastUtil toastUtil = new ToastUtil();
    private ListView pull_dingdan;
    /**
     * 没有商品
     */
    private TextView tv_none;
    private TextView tv_noPrint;
    private int offset = 0;
    private int page = 500;
    private Button btn_yijiantongzhi;
    /**
     * 打印信息的集合
     */
    private ArrayList<OrderEntity> dayingslist;
    // Name of the connected device
    private String mConnectedDeviceName = null;
    // String buffer for outgoing messages
    private StringBuffer mOutStringBuffer;
    // Local Bluetooth adapter
    private BluetoothAdapter mBluetoothAdapter = null;
    private String orderno;
    private String order_time;
    private String book_time;
    private String total;
    private String name;
    private String phone;
    private String shuliang;
    private String totalamount;
    private String dayno;
    private Context mContext;
    // The Handler that gets information back from the BluetoothService
    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_STATE_CHANGE:
                    switch (msg.arg1) {
                        case BluetoothService.STATE_CONNECTED:
                            break;
                        case BluetoothService.STATE_CONNECTING:
                            break;
                        case BluetoothService.STATE_LISTEN:
                        case BluetoothService.STATE_NONE:
                            break;
                        default:
                            break;
                    }
                    break;
                case MESSAGE_WRITE:
                    break;
                case MESSAGE_READ:
                    break;
                case MESSAGE_DEVICE_NAME:
                    mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
                    Toast.makeText(mContext, "连接至"
                            + mConnectedDeviceName, Toast.LENGTH_SHORT).show();
                    break;
                case MESSAGE_TOAST:
                    Toast.makeText(getActivity(), msg.getData().getString(TOAST),
                            Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContext = getActivity();
        return inflater.inflate(R.layout.fragment_order, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        initRes();

    }

    @Override
    public void onStart() {
        super.onStart();
//		handler.postDelayed(runnable, 10000);
        if (!mBluetoothAdapter.isEnabled()) {
            //打开蓝牙
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        }
        if (mService == null) {
            mService = new BluetoothService(getActivity(), mHandler);
        }
    }

    private void initRes() {
        SharedPreferences sp = getActivity().getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        token = sp.getString("data", "");
        btn_yijiantongzhi = (Button) getView().findViewById(R.id.btn_yijiantongzhi);
        pull_dingdan = (ListView) getView().findViewById(
                R.id.pull_dingdan);
        tv_noPrint = (TextView) getView().findViewById(R.id.tv_noPrint);

        tv_none = (TextView) getView().findViewById(R.id.tv_none);

        getDingdaninfo();

        btn_yijiantongzhi.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
//				Yijiantongzhi();
                tongzhidialog();
            }
        });
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        // If the adapter is null, then Bluetooth is not supported
        if (mBluetoothAdapter == null) {
            Toast.makeText(getActivity(), "您的设备不支持蓝牙", Toast.LENGTH_SHORT).show();
//			finish();
            return;
        }
    }

    /**
     * 获取订单列表
     */
    private void getDingdaninfo() {
        if (!NetWorkUtil.isNetwork(getActivity())) {
            toastUtil.Short(getActivity(), "  " + "当前网络不可用")
                    .setToastBackground(Color.WHITE, 0).show();
            return;
        }
        dayingslist = new ArrayList<OrderEntity>();

        toolModel = new HttpTools(getActivity());
        toolModel.getDyOrders(token, page + "", offset + "", "3", new JsonDingdan());
    }

    /**
     * 打印
     *
     * @param message
     */
    private void sendMessage(String message) {
        if (message.length() > 0) {
            // Get the message bytes and tell the BluetoothService to write
            byte[] send;
            try {
                send = message.getBytes("GB2312");
            } catch (UnsupportedEncodingException e) {
                send = message.getBytes();
            }

            mService.write(send);
        }
    }


    private void sendMessage(Bitmap bitmap) {
        // Check that we're actually connected before trying anything
        if (mService.getState() != BluetoothService.STATE_CONNECTED) {
            Toast.makeText(getActivity(), "蓝牙没有连接", Toast.LENGTH_SHORT).show();
            return;
        }
        // 发送打印图片前导指令
        byte[] start = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1B,
                0x40, 0x1B, 0x33, 0x00};
        mService.write(start);

        /**获取打印图片的数据**/
//		byte[] send = getReadBitMapBytes(bitmap);

        mService.printCenter();
        byte[] draw2PxPoint = PicFromPrintUtils.draw2PxPoint(bitmap);

        mService.write(draw2PxPoint);
        // 发送结束指令
        byte[] end = {0x1d, 0x4c, 0x1f, 0x00};
        mService.write(end);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_CONNECT_DEVICE:
                // When DeviceListActivity returns with a device to connect
                if (resultCode == Activity.RESULT_OK) {
                    // Get the device MAC address
                    String address = data.getExtras()
                            .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                    // Get the BLuetoothDevice object
                    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
                    // Attempt to connect to the device
                    mService.connect(device);
                }
                break;
            case REQUEST_ENABLE_BT:
                // When the request to enable Bluetooth returns
                if (resultCode == Activity.RESULT_OK) {
//                setupChat();
                    Toast.makeText(getActivity(), "蓝牙已打开", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getActivity(), "蓝牙没有打开", Toast.LENGTH_SHORT).show();
//                finish();
                }
                break;
                default:
                    break;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.scan:
                // Launch the DeviceListActivity to see devices and do scan
                Intent serverIntent = new Intent(getActivity(), DeviceListActivity.class);
                startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
                return true;
            case R.id.disconnect:
                // disconnect
                mService.stop();
                return true;
                default:
                    break;
        }
        return false;
    }

    /**
     * 打印内容
     */
    private void print() {
        sendMessage(" \n");
        sendMessage(" \n");
        sendMessage(" \n");
        sendMessage(" \n");
        sendMessage(printFormatUtil.printTitle("                 智慧食堂") + "\n");
        sendMessage(printFormatUtil.printTitle("************************************************") + "\n");
        sendMessage("名称" + "             " + "数量" + "              " + "价格" + " \n");
        mService.printSize(2);
        menuMsgMap.put("", gonds);
        sendMessage(printFormatUtil.printMenuMSG(menuMsgMap));
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = sDateFormat.format(new java.util.Date());
        mService.print(3);
        sendMessage("数量：" + totalamount + "份" + "   ");
        sendMessage("总价：" + total + "元" + " \n");
        sendMessage("订单编号：" + orderno + " \n");
        sendMessage("姓名：" + name + " \n");
        sendMessage("联系电话：" + phone + " \n");
        mService.printSize(2);
        sendMessage("编号：" + dayno + " \n");
        mService.print(3);
        sendMessage(printFormatUtil.printTitle("***********************************************") + "\n");
        sendMessage(printFormatUtil.printTitle("             请妥善保管好购物凭证" + "\n"));
        sendMessage(printFormatUtil.printTitle("                 谢谢惠顾！") + "\n");
        sendMessage(" \n");
        sendMessage(" \n");
        sendMessage(" \n");
        sendMessage(" \n");
        sendMessage(" \n");
    }

    /**
     * T1mini打印
     */
    private void miniPrint() {
        sendMessage(" \n");
        sendMessage(" \n");
        sendMessage(" \n");
        sendMessage(" \n");
        sendMessage(printFormatUtil.printTitle("    智慧食堂") + "\n");
        sendMessage(printFormatUtil.printTitle("********************************") + "\n");
        sendMessage("名称" + "         " + "数量" + "           " + "价格" + " \n");
        mService.printSize(3);
        menuMsgMap.put("", gonds);
        sendMessage(printFormatUtil.printMenuMSG(menuMsgMap));
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = sDateFormat.format(new java.util.Date());
//        mService.print(3);
        sendMessage("数量：" + totalamount + "份" + "   ");
        sendMessage("总价：" + total + "元" + " \n");
        sendMessage("订单编号：" + orderno + " \n");
        sendMessage("姓名：" + name + " \n");
        sendMessage("联系电话：" + phone + " \n");
        mService.printSize(2);
        sendMessage("编号：" + dayno + " \n");
        mService.print(3);
        sendMessage(printFormatUtil.printTitle("********************************") + "\n");
        sendMessage(printFormatUtil.printTitle("       请妥善保管好购物凭证" + "\n"));
        sendMessage(printFormatUtil.printTitle("          谢谢惠顾！") + "\n");
        sendMessage(" \n");
        sendMessage(" \n");
        sendMessage(" \n");
        sendMessage(" \n");
        sendMessage(" \n");
    }

    /**
     * 一键通知弹窗
     */
    private void tongzhidialog() {

        final Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        iscatview = LayoutInflater.from(getActivity()).inflate(
                R.layout.dialog_pay, null);
        TextView tital = (TextView) iscatview.findViewById(R.id.tital);
        TextView tv_no = (TextView) iscatview.findViewById(R.id.tv_no);
        TextView tv_yes = (TextView) iscatview.findViewById(R.id.tv_yes);
        tital.setText("是否通知所有用户?");
        dialog.setContentView(iscatview);
        dialog.show();
        tv_no.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tv_yes.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
//				Yijiantongzhi();
                dialog.dismiss();
            }
        });
    }

    private class JsonDingdan extends JsonHttpResponseHandler {

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
            Log.w("ORDER", response.toString());
            try {
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
                    JSONObject data = response.getJSONObject("data");
                    int unprintnum = data.getInt("unprintnum");
                    tv_noPrint.setText("未打印人数:" + unprintnum);
                    JSONArray orders = data.getJSONArray("orders");
                    for (int i = 0; i < orders.length(); i++) {
                        JSONObject object = orders.getJSONObject(i);
                        String id = object.getString("id");
                        String name = object.getString("name");
                        String orderno = object.getString("dayno");
                        String total = object.getString("total");
                        String print = object.getString("print");
                        String notice = object.getString("notice");
                        String refund = object.getString("refund");
                        String order_time = object.getString("order_time");
                        dayingslist.add(new OrderEntity(id, orderno, name, total, order_time, refund, print, notice));
                    }
                    if (dayingslist.size() == 0) {
                        tv_none.setVisibility(View.VISIBLE);
                    } else {
                        tv_none.setVisibility(View.GONE);
                    }
                    dayingAdapter = new DayingAdapter(getActivity(), dayingslist);
                    pull_dingdan.setAdapter(dayingAdapter);
                    pull_dingdan.setDivider(null);
                } else {
                    Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                }


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            super.onFailure(statusCode, headers, throwable, errorResponse);
            Toast.makeText(getActivity(),
                    "网络请求失败", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 打印适配器
     */
    public class DayingAdapter extends BaseAdapter {
        Context context;
        ArrayList<OrderEntity> dingdanList;
        HttpTools httpTools = null;
        View iscatview = null;

        public DayingAdapter(Context context, ArrayList<OrderEntity> dingdanList) {
            super();
            this.context = context;
            this.dingdanList = dingdanList;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return dingdanList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return dingdanList.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(final int position, View view, ViewGroup parent) {
            final ViewHandle handle;
            if (view == null) {
                handle = new ViewHandle();
                view = LayoutInflater.from(context).inflate(R.layout.item_daying, null);
                handle.tv_bianhao = (TextView) view.findViewById(R.id.tv_order);
                handle.tv_name = (TextView) view.findViewById(R.id.tv_name);
                handle.tv_jine = (TextView) view.findViewById(R.id.tv_jine);
                handle.tv_shijian = (TextView) view.findViewById(R.id.tv_time);
                handle.btn_tuikuan = (Button) view.findViewById(R.id.btn_tuikuan);
                handle.btn_tongzhi = (Button) view.findViewById(R.id.btn_tongzhi);
                view.setTag(handle);
            } else {
                handle = (ViewHandle) view.getTag();
            }

            handle.tv_bianhao.setText(dingdanList.get(position).getOrderno());
            handle.tv_name.setText(dingdanList.get(position).getName());
            handle.tv_jine.setText("￥" + dingdanList.get(position).getTotal());
            handle.tv_shijian.setText(dingdanList.get(position).getTime());

            if ("1".equals(dingdanList.get(position).getPrint())) {
                handle.btn_tuikuan.setText("已打印");
//				dayingdialog(position);
            } else {
                handle.btn_tuikuan.setText("打印");
//				Daying(position);
            }
            if ("1".equals(dingdanList.get(position).getNotice())) {
                handle.btn_tongzhi.setText("已通知");
                handle.btn_tongzhi.setEnabled(false);
//				dayingdialog(position);
            } else {
                handle.btn_tongzhi.setEnabled(true);
                handle.btn_tongzhi.setText("通知");
//				Daying(position);
            }
            handle.btn_tuikuan.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (mService.getState() != BluetoothService.STATE_CONNECTED) {
                        Toast.makeText(getActivity(), "蓝牙没有连接", Toast.LENGTH_SHORT).show();
                        Printdialog();
                    } else {
                        if ("1".equals(dingdanList.get(position).getPrint())) {
                            dayingdialog(position);
                        } else {
                            Daying(position, handle.btn_tuikuan);
                        }

                    }

                }
            });
            handle.btn_tongzhi.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
//					Tongzhi(position,handle.btn_tongzhi);
                }
            });

            return view;
        }

        /**
         * 是否打印弹窗
         */
        private void Printdialog() {

            final Dialog dialog = new Dialog(context);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

            iscatview = LayoutInflater.from(context).inflate(
                    R.layout.dialog_pay, null);
            TextView tital = (TextView) iscatview.findViewById(R.id.tital);
            TextView tv_no = (TextView) iscatview.findViewById(R.id.tv_no);
            TextView tv_yes = (TextView) iscatview.findViewById(R.id.tv_yes);
            tital.setText("蓝牙未连接，是否连接打印机?");
            dialog.setContentView(iscatview);
            dialog.show();
            tv_no.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            tv_yes.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent serverIntent = new Intent(getActivity(), DeviceListActivity.class);
                    startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
                    dialog.dismiss();
                }
            });
        }
        /**通知*/

        /**
         * 打印弹窗
         */
        private void dayingdialog(final int index2) {

            final Dialog dialog = new Dialog(context);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

            iscatview = LayoutInflater.from(context).inflate(
                    R.layout.dialog_pay, null);
            TextView tital = (TextView) iscatview.findViewById(R.id.tital);
            TextView tv_no = (TextView) iscatview.findViewById(R.id.tv_no);
            TextView tv_yes = (TextView) iscatview.findViewById(R.id.tv_yes);
            tital.setText("是否重复打印?");
            dialog.setContentView(iscatview);
            dialog.show();
            tv_no.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            tv_yes.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
//    				Tuikuan(index2);
                    Dingdandetial(index2);
                    dialog.dismiss();
                }
            });
        }

        private void Daying(final int index, final Button btn_daying) {
            if (httpTools == null) {
                httpTools = new HttpTools(context);
            }
            httpTools.printOrder(token, dingdanList.get(index).getId(), new JsonHttpResponseHandler() {

                @Override
                public void onSuccess(int statusCode, Header[] headers,
                                      JSONObject response) {
                    super.onSuccess(statusCode, headers, response);
                    try {
                        boolean code = response.getBoolean("code");
                        String msg = response.getString("msg");
                        if (code == true) {
                            JSONObject data = response.getJSONObject("data");
                            int unprintnum = data.getInt("unprintnum");
                            tv_noPrint.setText("未打印人数:" + unprintnum);
                            Dingdandetial(index);
                            btn_daying.setText("已打印");
                            dingdanList.get(index).setPrint("1");
                        } else {
                            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    super.onFailure(statusCode, headers, responseString, throwable);
                    Log.w("PRINT", responseString);
                }
            });
        }

        /**
         * 订单详情
         */
        private void Dingdandetial(final int index) {
            detaillist = new ArrayList<GoodsInfoBean>();
            menuMsgMap = new LinkedHashMap<String, LinkedList<String>>();
            gonds = new LinkedList<String>();
            shuliang = "";
//    		SharedPreferences sp = getActivity().getSharedPreferences("spReg",
//    				Context.MODE_PRIVATE);
//    		token = sp.getString("token", "");
            Log.i("zq", "orderid" + dingdanList.get(index).getId());
            if (httpTools == null) {
                httpTools = new HttpTools(getActivity());
            }
            httpTools.getDyDetail(token, "", "", dingdanList.get(index).getId(), new JsonOrderDetail());
        }

        class ViewHandle {
            /**
             * 订单编号
             */
            TextView tv_bianhao;
            /**
             * 姓名
             */
            TextView tv_name;
            /**
             * 价格
             */
            TextView tv_jine;
            /**
             * 时间
             */
            TextView tv_shijian;
            /**
             * 打印
             */
            Button btn_tuikuan;
            /**
             * 通知
             */
            Button btn_tongzhi;
        }

        private class JsonOrderDetail extends JsonHttpResponseHandler {
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Log.e("zq", "orderdetail---" + response.toString());
                try {
                    boolean code = response.getBoolean("code");
                    if (code == true) {
                        JSONObject object = response.getJSONObject("data");
                        JSONArray array = object.getJSONArray("details");
                        dayno = object.getString("dayno");
                        orderno = object.getString("orderno");
                        order_time = object.getString("order_time");
                        book_time = object.getString("book_time");
//    					note = object.getString("note");
                        total = object.getString("total");
                        name = object.getString("name");
                        phone = object.getString("username");
                        totalamount = object.getString("totalamount");
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject object2 = array.getJSONObject(i);
                            String id = object2.getString("id");
                            String name = object2.getString("name");
                            String amount = object2.getString("amount");
                            String price = object2.getString("price");
                            String content = object2.getString("content");
                            String total = object2.getString("total");
//    						shuliang +=Integer.parseInt(amount);
                            double jiage = Double.parseDouble(amount) * Double.parseDouble(price);
                            detaillist.add(new GoodsInfoBean(name, amount, price, content, ""));
                            gonds.add(" \n" + name);
                            gonds.add("*" + amount);
                            gonds.add("" + total);
                        }
                        if (MyApplication.subModel.equals("T1mini")) {
                            miniPrint();
                        } else {
                            print();
                        }
                    } else {
                        String msg = response.getString("msg");
                        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable, errorResponse);
                Toast.makeText(getActivity(),
                        "网络请求失败", Toast.LENGTH_SHORT).show();
            }
        }

    }
}
